
// antd Upload 组件，自定义上传函数, 上传七牛云 customRequest <Upload customRequest={uploadFile}>
const uploadFile = async (options) => {
    const { file, onSuccess, onError, onProgress, onCancel } = options;
    const key = uuid.v4(); // 使用uuid避免文件重名
    const token = await getQiniuToken();
    // file：文件对象 key：文件名 token：七牛云上传需要的token
    const observable = qiniu.upload(file, key + file.name, token);
    observable.subscribe({
      next(res) {
        // 分段上传实时进度
        console.log('上传中', res);
        onProgress(res.total);
      },
      error(err) {
        console.log(err, '上传失败');
        // 上传失败时服务器的返回结果
        onError(err);
        // ...
      },
      complete(res) {
        // ...
        console.log(res, '上传成功');
        // 上传成功时服务器的返回结果
        onSuccess(res);
      },
    });
  };



// antd Upload 组件，自定义上传函数, 普通上传-axios customRequest <Upload customRequest={uploadFile}>
type ProgressTypes = {
  onUploadProgress?: (event: ProgressEvent) => void
}

export const uploadProps = {
  customRequest: (options: any) => {
    const { file, onSuccess, onError, onProgress } = options;
    // file：文件对象 key：文件名 token：七牛云上传需要的token
    const option: ProgressTypes = {
      onUploadProgress: (event: ProgressEvent) => {
        // 上传进度发生变化时调用param.progress
        onProgress(event.loaded / event.total * 100)
      }
    }
    uploadFileByQiniu({ file }, option).then(res => {
      if (res.code != 200) {
        onError(res.msg)
      }
      onSuccess(res)
    }).catch(err => {
      onError(err)
    })
  }

}